package com.hlf.modules.api.service.impl;

import com.hlf.modules.api.mapper.TokenMapper;
import com.hlf.modules.api.entity.TokenEntity;
import com.hlf.modules.api.service.TokenService;
import org.springframework.stereotype.Service;


import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service("tokenService")
public class TokenServiceImpl implements TokenService {
    @Autowired
    private TokenMapper tokenMapper;
    //12小时后过期
    private static final Long EXPIRE = 3600 * 24 * 30 * 2L;

    @Override
    public TokenEntity queryByUserId(Long userId) {
        return tokenMapper.queryByUserId(userId);
    }

    @Override
    public TokenEntity queryByToken(String token) {
        return tokenMapper.queryByToken(token);
    }

    @Override
    public void save(TokenEntity token) {
        tokenMapper.save(token);
    }

    @Override
    public void update(TokenEntity token) {
        tokenMapper.update(token);
    }

    @Override
    public Map<String, Object> createToken(long userId) {
        //生成一个token
        String token = UUID.randomUUID().toString();

        token = "123456";


        //当前时间
        Date now = new Date();

        //过期时间
        Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

        //判断是否生成过token
        TokenEntity tokenEntity = queryByUserId(userId);
        if (tokenEntity == null) {
            tokenEntity = new TokenEntity();
            tokenEntity.setUserId(userId);
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);

            //保存token
            save(tokenEntity);
        } else {
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);

            //更新token
            update(tokenEntity);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", EXPIRE);

        return map;
    }
}
